/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import persistencia.controller.jpa.CategoriaJpaController;
import persistencia.controller.jpa.RegistroJpaController;
import persistencia.entidade.Categoria;
import persistencia.entidade.Registro;

/**
 *
 * @author Novo usuário
 */
public class Importador {

    public static void main(String[] args) throws FileNotFoundException, IOException, Exception {

        Importador i = new Importador();
        i.doImport();
    }

    public void doImport() throws Exception {
        File f = new File("C:\\oficina\\moblecatalog\\MobCat_off_lwuit\\src\\data");
        File[] fs = f.listFiles();
        RegistroJpaController jpa = new RegistroJpaController();
        CategoriaJpaController cjpa = new CategoriaJpaController();
        Categoria cat = cjpa.findCategoria(23);
        List l = new LinkedList();
        l.add(cat);


        for (int i = 0; i < fs.length; i++) {
            File file = fs[i];
            if (file.getName().contains(".reg")) {
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                String s;
                while ((s = br.readLine()) != null) {
                    RegistroImport ri = new RegistroImport(s);
                    ri.setCategoriaCollection(l);
                    jpa.create(ri);
                }
            }
        }
    }

    public class RegistroImport extends Registro {

        public RegistroImport(String stream) {
            String[] strs = split(stream, "|");
            //id = Integer.parseInt(strs[0]);
            setNome(strs[1]);
            setCidade(strs[2]);
            setDdd(strs[3]);
            setBairro(strs[4]);
            setEmail(strs[5]);
            setEndereco(strs[6]);
            setPeso(Integer.parseInt(strs[7]));
            setResumoMob(strs[8]);
            setSite(strs[9]);
            setTelefone(strs[10]);
            setTelefone2(strs[11]);
            setUf(strs[12]);
        }

        public String[] split(String original, String separator) {
            Vector nodes = new Vector();

            // Parse nodes into vector
            int index = original.indexOf(separator);
            while (index >= 0) {
                nodes.addElement(original.substring(0, index));
                original = original.substring(index + separator.length());
                index = original.indexOf(separator);
            }
            // Get the last node
            nodes.addElement(original);

            // Create splitted string array
            String[] result = new String[nodes.size()];
            if (nodes.size() > 0) {
                for (int loop = 0; loop < nodes.size(); loop++) {
                    result[loop] = (String) nodes.elementAt(loop);
                }
            }
            return result;
        }
    }
}
